package com.tn.service.weigh.mq;


import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.tn.controller.alarm.common.model.weigh.mq.Trace;
import com.tn.dao.mbpdao.TaskInfoDispatch;
import com.tn.mqtt.AbstractBaseCallback;
import com.tn.taskdispatch.TaskInfoDispatchService;
import com.tn.utils.JsonUtil;
import com.tn.utils.redis.RedisUtils;
import com.tn.websocket.WebSocket;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: Suzuki
 * @CreateTime: 2022/10/19-17:21
 * @Description: 车牌号mq
 * @Version: 1.0
 **/
@Component
@Slf4j
public class SflocMQ extends AbstractBaseCallback {

    private final static String topic = "/rpm/sfloc";

    @Resource
    private WebSocket webSocket;

    @Autowired
    private TaskInfoDispatchService taskInfoDispatchService;




    @Override
    public void consumer(String topic, String msg) {

        Map map = new HashMap<>();
        map.put("72605333016", "X-HBF00016");//58
        map.put("72605332633", "3-HBF00366");//9  72605332633
        map.put("72605331296", "3-HBF00361");//14
        map.put("72605332586", "鄂B3-00706");//1号车
        map.put("72605333445", "3-HBF00365");//13  72605333445
        map.put("72605332544", "X-HBF00017");//59
        map.put("72605331331", "3-HBF00355");//8
        map.put("72605332623", "3-HBF00348");//10
        map.put("72605332631", "3-HBF00350");//11  72605332631
        map.put("72605331289", "3-HBF00349");//7
        map.put("72605332563", "3-HBF00363");//5
        map.put("72605333023", "3-HBF00351");//3
        map.put("72605332610", "3-HBF00360");//4

        Map msgMap = null;
        try {
            msgMap = JsonUtil.string2Obj(msg, Map.class);
            String uniqueId = msgMap.get("uniqueId").toString();
            String newUniqueId = uniqueId.substring(1);
            if (map.containsKey(newUniqueId)) {
                String license = map.get(newUniqueId).toString();
                msgMap.put("license", license);
                Trace trace = new Trace();
                trace.setLicense(license);
                trace.setUniqueId(uniqueId);
                trace.setX(msgMap.get("lat").toString());
                trace.setY(msgMap.get("lon").toString());
                if (msgMap.get("z") == null) {
                    trace.setZ("0");
                    trace.setPosition(trace.getX() + "," + trace.getY() + "," + trace.getZ());
                } else {
                    trace.setZ(msgMap.get("z").toString());
                    trace.setPosition(trace.getX() + "," + trace.getY() + "," + trace.getZ());
                }
                //查询铲车对应的任务货品信息
                TaskInfoDispatch taskInfoDispatches=new TaskInfoDispatch();
                ObjectMapper mapper = new ObjectMapper();
                String message = RedisUtils.getCacheObject("loader_car_task");
                Map<String, List<TaskInfoDispatch>> taskInfoDispatches1=null;
                taskInfoDispatches1 = mapper.readValue(message, new TypeReference<Map<String, List<TaskInfoDispatch>>>() {});
                if (!CollectionUtils.isEmpty(taskInfoDispatches1)) {
                    for (String s : taskInfoDispatches1.keySet()) {
                        if (s.contains(newUniqueId)) {
                            List<TaskInfoDispatch> taskInfoDispatches2 = taskInfoDispatches1.get(s);
                            if (!CollectionUtils.isEmpty(taskInfoDispatches2)) {
                                taskInfoDispatches = taskInfoDispatches2.get(0);
                                msgMap.put("taskInfo", taskInfoDispatches);
                                break;
                            }else {
                                msgMap.put("taskInfo", null);
                                break;
                            }

                        }
                    }
                }

                webSocket.sendOneMessage("hs01", JsonUtil.obj2String(msgMap));
                //influxBean.write(trace);
                return;
            }
            ObjectMapper mapper = new ObjectMapper();
            String message = RedisUtils.getCacheObject("truck_tag");
            List<Map<String, String>> licenseTagList=null;
            licenseTagList = mapper.readValue(message, new TypeReference<List<Map<String, String>>>() {});

            if (CollectionUtils.isEmpty(licenseTagList)) {
                return;
            }
            for (Map<String, String> licenseTagMap : licenseTagList) {
                String tagId = licenseTagMap.get("tagid");
                String license = licenseTagMap.get("license");
                if (tagId.equals(newUniqueId)) {
                    msgMap.put("license", license);
                    //查询货车任务
                    //producerDao.selectTruckTaskInfo(license);
                    webSocket.sendOneMessage("hs01", JsonUtil.obj2String(msgMap));
                    Trace trace = new Trace();
                    trace.setLicense(license);
                    trace.setUniqueId(uniqueId);
                    if (msgMap.get("z") == null) {
                        trace.setPosition(msgMap.get("lat").toString() + "," + msgMap.get("lon").toString() + "," + "0.0");
                    } else {
                        trace.setZ(msgMap.get("z").toString());
                        trace.setPosition(msgMap.get("lat").toString() + "," + msgMap.get("lon").toString() + "," + trace.getZ());
                    }
                    // influxBean.write(trace);
                    break;
                }

            }
        } catch (Exception e) {
           throw new RuntimeException();
        }


    }

    @Override
    protected AbstractBaseCallback getClazz() {
        return this;
    }

    @Override
    protected String getTopic() {
        return topic;
    }
}
